﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EscButtonClickController : MonoBehaviour {

    public GameObject go_isOver;
    public Transform tf_duihuakuang;
    public Image i_shiyan;
    public Sprite s_close;

	// Use this for initialization
	void Start () {
        EventManager<string>.AddEvent(MyEventType.OnllyEscButtonEvent,OnEscButtonClick);
        EventManager<bool>.AddEvent(MyEventType.OnllyEnterButtonClickEvent, OnDuihuakuangEnter);
        EventManager<string>.AddEvent(MyEventType.OnllyDirButtonClickEvent, OnDuihuakuangDir);
    }

    private void OnDuihuakuangEnter(bool obj)
    {
        if (OnllyUIController._instance.b_isDuihuakuang && OnllyPage2UIController._instance.testType == "手控" && OnllyPage2UIController._instance.b_shoukongStart) {
            
            if (tf_duihuakuang.GetChild(0).GetChild(0).gameObject.activeSelf)
            {
                OnllyPage1UIController.Instance.b_isDongzuo = false;
                i_shiyan.sprite = s_close;
                //ShijianJiluUIController._instance.b_canStart = false;
                TimerMngr.Instance.Create(()=> {
                    OnllyPage2UIController._instance.b_shoukongStart = false;
                    OnllyUIController._instance.b_isDuihuakuang = false;
                }).Start(0.1f);  
            }
            else
            {
                TimerMngr.Instance.Create(() =>
                {
                    OnllyUIController._instance.b_isDuihuakuang = false;
                }).Start(0.1f);              //防止此时对话框以外的交互进行
                OnDuihuakuangDir("left");
            }
            go_isOver.SetActive(false);
        }
    }

    private void OnEscButtonClick(string obj)
    {
        if (obj == "手控" && OnllyPage2UIController._instance.b_shoukongStart && !OnllyUIController._instance.b_isDuihuakuang) {
            OnllyUIController._instance.ChangeZhishideng(false, 0.2f);
            //出现是否退出试验弹框
            go_isOver.SetActive(true);
            OnllyUIController._instance.b_isDuihuakuang = true;
        }
    }

    /// <summary>
    /// 是否结束试验对话框的左右操作
    /// </summary>
    /// <param name="dir"></param>
    void OnDuihuakuangDir(string dir)
    {
        if (OnllyUIController._instance.b_isDuihuakuang && OnllyPage2UIController._instance.testType == "手控")
        {
            switch (dir)
            {
                case "left":
                    tf_duihuakuang.GetChild(0).GetChild(0).gameObject.SetActive(true);
                    tf_duihuakuang.GetChild(0).GetChild(1).GetComponent<Text>().color = Color.white;
                    tf_duihuakuang.GetChild(1).GetChild(0).gameObject.SetActive(false);
                    tf_duihuakuang.GetChild(1).GetChild(1).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
                    break;
                case "right":
                    tf_duihuakuang.GetChild(0).GetChild(0).gameObject.SetActive(false);
                    tf_duihuakuang.GetChild(0).GetChild(1).GetComponent<Text>().color = new Color32(15, 31, 38, 255);
                    tf_duihuakuang.GetChild(1).GetChild(0).gameObject.SetActive(true);
                    tf_duihuakuang.GetChild(1).GetChild(1).GetComponent<Text>().color = Color.white;
                    break;
                default: break;
            }
        }
    }
}
